$snapinName = "Microsoft.SharePoint.PowerShell"
if( ( Get-PSSnapin | ? { $_.Name -eq $snapinName } ) -eq $null ) {
    Write-Host -ForegroundColor Yellow "Loading SharePoint Snapin"
    Add-PsSnapin $snapinName
}

Write-Host -ForegroundColor Yellow "Building"
$msbuild = "c:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe"
& $msbuild (Resolve-Path "..\SPConnections.SandboxingDemo\SPConnections.SandboxingDemo.csproj") /target:Clean /target:CleanPackage /target:Build /target:Package /verbosity:quiet /nologo
if($_ -ne $null) {
    Exit
}
$siteUrl = "http://$env:COMPUTERNAME"

Write-Host -ForegroundColor Yellow "Verifying Web Application readyness"
$webApplication = Get-SPWebApplication $siteUrl -ErrorAction SilentlyContinue
if($webApplication -eq $null) {
    Write-Host -ForegroundColor Red "There is not web application created at $siteUrl. Create one using central administration."
}

$site = Get-SPSite $siteUrl -ErrorAction SilentlyContinue
if($site -ne $null)
{
	Write-Host -ForegroundColor Yellow "Deleting old site"
    Remove-SPSite $site -Confirm:$false
}
Write-Host -ForegroundColor Yellow "Creating new site"
New-SPSite -Url $siteUrl -Language 1033 -Name "DemoSite" -OwnerAlias "$env:USERDOMAIN\$env:USERNAME" | Out-Null
Write-Host -ForegroundColor Yellow "Installing Demo"
Add-SPUserSolution -LiteralPath (Resolve-Path "..\SPConnections.SandboxingDemo\bin\debug\SPConnections.SandboxingDemo.wsp") -Site $siteUrl | Out-Null
Install-SPUserSolution "SPConnections.SandboxingDemo.wsp" -Site $siteUrl -Confirm:$false | Out-Null
Write-Host -ForegroundColor Yellow "Enabling Demo"
$site = Get-SPWeb $siteUrl
$site.ApplyWebTemplate("{51148101-7d9a-4410-8fbc-39248bc96071}#DemoSite")
$site.CreateDefaultAssociatedGroups($null, $null, $null)

$pagesList = $site.Lists.Item("DemoPages")
[Microsoft.SharePoint.Utilities.SPUtility]::CreateNewWikiPage(
	$pagesList, [Microsoft.SharePoint.Utilities.SPUrlUtility]::CombineUrl(
		$site.ServerRelativeUrl, "DemoPages/test.aspx")) | Out-Null
[Microsoft.SharePoint.Utilities.SPUtility]::CreateNewWikiPage(
	$pagesList, [Microsoft.SharePoint.Utilities.SPUrlUtility]::CombineUrl(
		$site.ServerRelativeUrl, "DemoPages/test2.aspx")) | Out-Null
[Microsoft.SharePoint.Utilities.SPUtility]::CreateNewWikiPage(
	$pagesList, [Microsoft.SharePoint.Utilities.SPUrlUtility]::CombineUrl(
		$site.ServerRelativeUrl, "DemoPages/test3.aspx")) | Out-Null

$childList = $site.Lists.Item("Child Items")
$item = $childList.AddItem()
$item.Item("Title") = "Child Item 1" 
$item.Item("ParentItem") = "1;#Parent Item 1"
$item.Update()

$item = $childList.AddItem()
$item.Item("Title") = "Child Item 2" 
$item.Item("ParentItem") = "1;#Parent Item 1"
$item.Update()

$item = $childList.AddItem()
$item.Item("Title") = "Child Item 3" 
$item.Item("ParentItem") = "2;#Parent Item 2"
$item.Update()

$item = $childList.AddItem()
$item.Item("Title") = "Child Item 4" 
$item.Item("ParentItem") = "2;#Parent Item 2"
$item.Update()

